package config;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.util.ArrayList;
import java.util.List;

public class GameConfig {
    private  int width;
    private  int height;
    private  int windowSize;
    private  int padding;
    private  String title;
    private  int windowUp;

    public String getTitle() {
        return title;
    }
    public int getWindowUp() {
        return windowUp;
    }
    public int getWidth() {
        return width;
    }
    public int getHeight() {
        return height;
    }
    public int getWindowSize() {
        return windowSize;
    }
    public int getPadding() {
        return padding;
    }
    public List<LayerConfig> getLayersConfig() {
        return layersConfig;
    }
    private List<LayerConfig> layersConfig;

    /**
     * 读取xml文件，获取全部游戏配置
     * @throws DocumentException
     */
    public  GameConfig() throws DocumentException {
        //创建xml读取器
        SAXReader reader = new SAXReader();
        //读取xml文件
        Document doc = reader.read("config/aaa.xml");
        //获取xml根节点
        Element game = doc.getRootElement();
        //配置窗口参数
        this.setUiConfig(game.element("frame"));
        //配置系统参数
        this.setSystemConfig(game.element("system"));
        //配置数据访问参数
        this.setDataConfig(game.element("data"));


    }

    /**
     * 配置窗口参数
     * @param frame
     */
    private void setUiConfig(Element frame) {
        this.width =  Integer.parseInt(frame.attributeValue("width"));
        this.height =  Integer.parseInt(frame.attributeValue("height"));
        this.windowSize =  Integer.parseInt(frame.attributeValue("windowSize"));
        this.padding =  Integer.parseInt(frame.attributeValue("padding"));
        this.title = frame.attributeValue("title");
        this.windowUp = Integer.parseInt(frame.attributeValue("windowUp"));

        List<Element> layers =   frame.elements("layer");
        layersConfig = new ArrayList<>();
        for (Element layer : layers) {
            LayerConfig lc = new LayerConfig(
                    layer.attributeValue("className"),
                    Integer.parseInt(layer.attributeValue("x")),
                    Integer.parseInt(layer.attributeValue("y")),
                    Integer.parseInt(layer.attributeValue("w")),
                    Integer.parseInt(layer.attributeValue("h"))
            );
            layersConfig.add(lc);
        }
    }

    /**
     * 配置系统参数
     * @param system
     */
    private void setSystemConfig(Element system) {

    }

    /**
     * 配置数据访问参数
     * @param data
     */
    private void setDataConfig(Element data) {

    }



}
